package com.huanletao.spring.util;

import com.huanletao.spring.aop.Log;
import com.huanletao.spring.aop.Permssion;
import com.huanletao.spring.data.pojo.Dog;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.util.ReflectionUtils;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * description: ResourcesUtilsUniles <br>
 * date: 2022/6/28 9:31 <br>
 * author: 踏雪无痕 <br>
 * version: 1.0 <br>
 */
public class ResourcesUtilsUniles {

    public static void main(String[] args) throws IOException {
        ResourcePatternResolver patternResolver = ResourcePatternUtils.getResourcePatternResolver(new PathMatchingResourcePatternResolver());

        Properties properties = readYamlConfigFileExample(patternResolver);
        System.out.println("object = " + properties);

        ClassPathResource resource1 = new ClassPathResource("configuration/config.properties");

        //就是将resource资源 填充到properties中,
        assert properties != null;
        PropertiesLoaderUtils.fillProperties(properties, resource1);

        System.out.println("object = " + properties);

        BindConfigurationValue annotation = AnnotationUtils.findAnnotation(Dog.class, BindConfigurationValue.class);
        Class<? extends Annotation> aClass = annotation.annotationType();
        System.out.println("aClass = " + aClass);

        String key = (String) AnnotationUtils.getValue(annotation, "value");

        Dog dog = new Dog();

        ReflectionUtils.doWithFields(Dog.class, field -> {
            BindConfigurationValue annotation1 = field.getAnnotation(BindConfigurationValue.class);

            Object value = AnnotationUtils.getValue(annotation1, "value");
            String property = properties.getProperty((String) value);
            field.setAccessible(true);
            field.set(dog, property);
        }, field -> field.isAnnotationPresent(BindConfigurationValue.class));

        System.out.println("dog = " + dog);

    }

    /**
     * 读取yaml 配置文件示例；
     *
     * @param patternResolver
     * @return
     */
    private static Properties readYamlConfigFileExample(ResourcePatternResolver patternResolver) {
        Resource resource = patternResolver.getResource("configuration/config.yaml");
        YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
        yamlFactory.setResources(resource);
        return yamlFactory.getObject();
    }


    /**
     * annotationUtils，使用示例；
     *
     *
     */
    private static void AnnotationUtilsExample() {
        //在类上获取注解
        Permssion permssion = AnnotationUtils.findAnnotation(com.huanletao.spring.aop.DefaultLoginServiceImpl.class, Permssion.class);
        System.out.println("permssion = " + permssion);
        Method login = ReflectionUtils.findMethod(com.huanletao.spring.aop.DefaultLoginServiceImpl.class, "login", String.class, String.class);

        //从方法中获取注解
        Log annotation = AnnotationUtils.findAnnotation(login, Log.class);
        System.out.println("annotation = " + annotation);

        //获取值
        Object title = AnnotationUtils.getValue(annotation, "title");
        System.out.println("title = " + title);
        //获取默认值
        Object defaultValue = AnnotationUtils.getDefaultValue(annotation, "isSaveRequestData");
        System.out.println("defaultValue = " + defaultValue);

        //获取所有属性
        Map<String, Object> annotationAttributes = AnnotationUtils.getAnnotationAttributes(annotation);
        System.out.println("annotationAttributes = " + annotationAttributes);
    }

    /**
     * 读取properties 配置文件示例；
     *
     *
     * @throws IOException
     */
    private static void propertiesLoaderUtilsExample() throws IOException {
        ResourcePatternResolver patternResolver = ResourcePatternUtils.getResourcePatternResolver(new PathMatchingResourcePatternResolver());

        Resource resource = patternResolver.getResource("configuration/config.properties");

        //加载properties 配置文件。
        Properties properties = PropertiesLoaderUtils.loadProperties(resource);

        Enumeration<String> enumeration = (Enumeration<String>) properties.propertyNames();
        Set<Map.Entry<Object, Object>> entries = properties.entrySet();
        for (Map.Entry<Object, Object> entry : entries) {
            System.out.println("key =" + entry.getKey() + " \t value = " + entry.getValue());
        }
        while (enumeration.hasMoreElements()) {
            String key = enumeration.nextElement();
            String property = properties.getProperty(key);
            System.out.println("property = " + property);
        }
    }
}
